﻿/*
 * This software is the confidential and proprietary
 * information of NetBay
 * Such confidential information should not be disclosed
 * NetBay copyright(c) 2010
 */

#region Using
using System;
using System.Collections.Generic;
using System.Data.Common;
using NetBay.Core.BusinessEntities;
using Rio.Framework.Historical;
using System.Data;
#endregion

namespace Rio.Doc.Business.Repository.Translator
{
    /// <summary>
    /// Class used to convert Index database row to Index object
    /// </summary>
    public class IndexTranslator
    {


        public static Dictionary<long, long> TranslateIndexPosition(IDataReader P_oDbDataReader)
        {
            Dictionary<long, long> l_dIndexRetour = new Dictionary<long, long>();

            try
            {
                using (P_oDbDataReader)
                {
                    while (P_oDbDataReader.Read())
                    {

                        l_dIndexRetour.Add(Convert.ToInt32(P_oDbDataReader["Position"]), Convert.ToInt32(P_oDbDataReader["rp_ch_no"]));
                    }
                }
            }
            catch (Exception ex)
            {
                NetBay.Core.Log.LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }

            return l_dIndexRetour;

        }
        
        
        /// <summary>
        /// Translate a bunch of Index DataReader to Index object
        /// </summary>
        /// <param name="P_oDbDataReader">The dataReader that contains the rows</param>
        /// <returns>A list of Index Object</returns>
        public static List<Index> TranslateIndex(IDataReader P_oDbDataReader)
        {
            var l_lstIndexes = new List<Index>();
            try
            {
                using (P_oDbDataReader)
                {
                    while (P_oDbDataReader.Read())
                    {
                        Index l_oIndex = new Index();
                        l_oIndex.Indentifier = Convert.ToInt32(P_oDbDataReader["RP_CH_NO"].ToString());
                        l_oIndex.Name = P_oDbDataReader["RP_CH_INDEX"].ToString();
                        //TODO vérifier l'utilité de ce champs
                        l_oIndex.IndexOrder = P_oDbDataReader["RP_CH_INDEX"].ToString();
                        l_oIndex.IndexLabel = P_oDbDataReader["RP_CH_NOM"].ToString();
                        l_oIndex.IsVisible =
                            Convert.ToBoolean(Convert.ToInt16(P_oDbDataReader["RP_CH_VISIBLE"].ToString()));
                        l_oIndex.IsMandatory =
                            Convert.ToBoolean(Convert.ToInt16(P_oDbDataReader["RP_CH_OBLIGATOIRE"].ToString()));
                        l_oIndex.ValueMaxLength = Convert.ToInt64(P_oDbDataReader["RP_CH_LONGUEUR"].ToString());
                        l_oIndex.DefaultValue = P_oDbDataReader["RP_CH_DEFAULT"].ToString();
                        l_oIndex.IsCaseSensitive =
                            Convert.ToBoolean(Convert.ToInt16(P_oDbDataReader["RP_CH_CASSE"].ToString()));
                        l_oIndex.Separator = P_oDbDataReader["RP_CH_SEPARATEUR"].ToString();
                        l_oIndex.Decimal = Convert.ToInt16(P_oDbDataReader["RP_CH_DECIMALE"].ToString());
                        l_oIndex.IsUnique =
                            Convert.ToBoolean(Convert.ToInt16(P_oDbDataReader["RP_CH_UNIQUE"].ToString()));
                        l_oIndex.Type = FieldRow.Translate<int>(P_oDbDataReader["IND_TYPE_ID"].ToString());

                        if (P_oDbDataReader["RP_CH_LIB_RANK"] != DBNull.Value)
                            l_oIndex.LabelRank = FieldRow.Translate<int>(P_oDbDataReader["RP_CH_LIB_RANK"].ToString());

                        //TODO reste categorie
                        l_lstIndexes.Add(l_oIndex);
                    }
                }
            }
            catch (Exception ex)
            {
                NetBay.Core.Log.LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }

            return l_lstIndexes;
        }
    }
}
